package com.sc.nft.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.sc.nft.dao.DigitalCollectionDao;
import com.sc.nft.dao.UserCollectionDao;
import com.sc.nft.entity.ApplicationRequirement;
import com.sc.nft.entity.DigitalCollection;
import com.sc.nft.entity.UserCollection;
import com.sc.nft.entity.UserInfo;
import com.sc.nft.entity.vo.ApplicationRequirementFactoryVO;
import com.sc.nft.enums.ApplicationRequirementEnum;
import com.sc.nft.service.ApplicationRequirementFactoryService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

import static com.sc.nft.enums.ApplicationRequirementEnum.HOLD_COLLECTION;


@Slf4j
@RequiredArgsConstructor
@Service
public class HoldCollectionServiceImpl implements ApplicationRequirementFactoryService {
    private final UserCollectionDao userCollectionDao;
    private final DigitalCollectionDao digitalCollectionDao;


    @Override
    public ApplicationRequirementFactoryVO applicationRequirement(UserInfo userInfo, ApplicationRequirement applicationRequirement, Integer type) {
        // 根据要求的条件获取用户持仓
        String value = applicationRequirement.getValue();
        List<String> split = StrUtil.split(value, ",");
        // 把split中的所有数据转换成Long类型
        List<Long> collectionIds = split.stream().map(Long::valueOf).collect(Collectors.toList());
        List<DigitalCollection> digitalCollections = digitalCollectionDao.getByIds(collectionIds);
        // 用户的持仓条件描述
        String holdCondition = assemblyHoldingConditions(digitalCollections);
        // 获取用户的持仓
        List<UserCollection> userCollections = userCollectionDao.getByProductIdsAndUserId(collectionIds, userInfo.getId());
        ApplicationRequirementFactoryVO applicationRequirementFactoryVO = new ApplicationRequirementFactoryVO();
        applicationRequirementFactoryVO.setKey(StrUtil.format("{}{}", applicationRequirement.getType().getDescription(), holdCondition));
        applicationRequirementFactoryVO.setAskForValue("1");
        if (CollUtil.isNotEmpty(digitalCollections)) {
            applicationRequirementFactoryVO.setCollectionNames(digitalCollections.stream().map(DigitalCollection::getFirstTitle).collect(Collectors.toList()));
        }
        applicationRequirementFactoryVO.setApplicationRequirementEnum(HOLD_COLLECTION);
        applicationRequirementFactoryVO.setQuantity(digitalCollections.size());
        applicationRequirementFactoryVO.setPracticalValue(Convert.toInt(CollUtil.isEmpty(userCollections)?0:userCollections.size()));
        applicationRequirementFactoryVO.setPracticalValueStr(Convert.toInt(CollUtil.isEmpty(userCollections)?0:userCollections.size()));
        applicationRequirementFactoryVO.setSatisfy(CollUtil.isNotEmpty(userCollections));
        return applicationRequirementFactoryVO;
    }
    @Override
    public Integer code() {
        return ApplicationRequirementEnum.HOLD_COLLECTION.getCode();
    }

    public String assemblyHoldingConditions(List<DigitalCollection> digitalCollections) {
//        根据digitalCollections中的firstTitle字段组装起来,使用逗号分隔
        return digitalCollections.stream().map(DigitalCollection::getFirstTitle).collect(Collectors.joining("/"));
    }
}
